package com.situ.blog.controller;

import com.situ.blog.pojo.entity.User;
import com.situ.blog.service.IUserService;
import com.situ.blog.service.impl.UserServiceImpl;
import com.situ.blog.util.JSONUtils;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user")
public class UserController extends HttpServlet {
    private IUserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "login":
                login(req, resp);
                break;
            case "loginout":
                loginout(req, resp);
                break;
            case "register":
                register(req,resp);
        }
    }

    private void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.login");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        //先判断验证码是不是正确
        //1、验证码错误，返回一个验证码错误的提示
        //2、验证码正确，再验证验证码是不是正确
        HttpSession session = req.getSession();
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            JSONUtils.toJSON(resp, Result.error("验证码错误"));
            return;
        }
        Integer count = userService.register(name, password);
        if (count == null || count == 0) {
            JSONUtils.toJSON(resp, Result.error("注册失败"));
        } else {
            JSONUtils.toJSON(resp, Result.ok("注册成功"));
            resp.sendRedirect("../page/login");
        }
    }

    private void loginout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.login");
        //清空用户信息
        req.getSession().removeAttribute("user");
        req.getSession().invalidate();
        //地址转发到登陆页面
        resp.sendRedirect("../page/login");

    }

    private void login(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserServlet.login");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        //先判断验证码是不是正确
        //1、验证码错误，返回一个验证码错误的提示
        //2、验证码正确，再验证验证码是不是正确
        HttpSession session = req.getSession();
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            JSONUtils.toJSON(resp, Result.error("验证码错误"));
            return;
        }

        User user = userService.login(name, password);
        if (user == null) {
            JSONUtils.toJSON(resp, Result.error("用户名或者密码错误"));
        } else {
            session.setAttribute("user", user);
            JSONUtils.toJSON(resp, Result.ok("登录成功"));
        }

    }
}
